<?php

namespace App\Http\Resources\Train\Screen;

use App\Models\Train\Stations\Repositories\StationRepository;
use App\Models\Train\Stations\TrainStation;
use App\Trip\Translate\Niutrans\Translation;
use Exception;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use JsonSerializable;

class WaitingResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param Request $request
     *
     * @return array|Arrayable|JsonSerializable
     * @throws Exception
     */
    public function toArray($request)
    {

        $res = [
            'train_code'    => $this->train_code ?? $this['train_code'],
            'station_code'  => $this->station_code ?? $this['station_code'],
            'start_time'    => $this->start_time ?? $this['start_time'],
            'checking_port' => (new Translation($this->checking_port ?? $this['checking_port']))->setTo(app()->getLocale())->translate()->getTranslation(),
            'waiting_state' => (new Translation($this->waiting_state ?? $this['waiting_state']))->setTo(app()->getLocale())->translate()->getTranslation(),
        ];


        $stationRepo            = new StationRepository(new TrainStation());
        $station                = $stationRepo->findOneByCode($res['station_code']);
        $res ['end_station']    = $station->getStationName();
        $res ['end_station_uy'] = $station->getStationNameUy();
        return $res;
    }
}
